/*******************************************************************************
 * Package: com.hngtrust.iir.user.controller
 * Type:    ViewHistoryController
 * Date:    2021/3/16 21:06
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.user.controller;

import com.hngtrust.framework.bind.ResponseInfo;
import com.hngtrust.framework.id.service.MyIdGenerator;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.iir.base.controller.vo.OptLogReqVO;
import com.hngtrust.iir.base.service.LogService;
import com.hngtrust.iir.company.service.CompanyService;
import com.hngtrust.iir.user.controller.dto.DeleteSearchVO;
import com.hngtrust.iir.user.controller.dto.DeleteViewHistoryReqVO;
import com.hngtrust.iir.user.controller.dto.ListViewHistory;
import com.hngtrust.iir.user.controller.dto.ViewHistoryRespVO;
import com.hngtrust.iir.user.dto.ListHistory;
import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.user.service.ViewHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 浏览历史管理
 *
 * @author Keqiwei
 * @date 2021/3/16 21:06
 */
@Slf4j
@Api(tags = "浏览历史管理")
@RestController
@RequestMapping("/view-history")
public class ViewHistoryController {
    @Autowired
    private ViewHistoryService viewHistoryService;
    
    @Autowired
    private MyIdGenerator myIdGenerator;
    @Autowired
    private CompanyService companyService;
    
    @Autowired
    private LogService logService;
    
    @Value("${tiger.minio.out.address}")
    private String minioAddress;
    
    @ApiOperation("浏览历史列表")
    @PostMapping("/list-view-history")
    public ResponseInfo<List<ViewHistoryRespVO>> listViewHistory(){
        
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        log.info("list-view-history  start ... userId:"+loginUser.getId());
        List<ListHistory> listHistories = viewHistoryService.queryViewHistory(loginUser.getId());
        if (listHistories.isEmpty()){
            return new ResponseInfo<>();
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<ViewHistoryRespVO> respVOList = new ArrayList<>();
        Map<String,List<ListViewHistory>> map = new LinkedHashMap<>();
        listHistories.forEach(v->{
            String date = format.format(v.getCreateTime());
            if(null != v.getLogo() && !v.getLogo().isEmpty()){
                v.setLogo(minioAddress + v.getLogo());
            }
            List<ListViewHistory> list = map.get(date);
            ListViewHistory vo = new ListViewHistory();
            BeanUtils.copyProperties(v,vo);
            if (list==null){
                list= new ArrayList<>();
                list.add(vo);
                map.put(date,list);
            }else {
                list.add(vo);
            }
        });
        
        map.forEach((key,value)->{
            ViewHistoryRespVO vo = new ViewHistoryRespVO();
            vo.setCreateTime(key);
            vo.setListHistories(value);
            respVOList.add(vo);
        });
        
        return new ResponseInfo<>(respVOList);
    }
    
    @ApiOperation("删除浏览历史")
    @PostMapping("/delete-view-history")
    public ResponseInfo<Object> deleteViewHistory(@ApiParam(value = "删除参数") @Valid @RequestBody DeleteViewHistoryReqVO vo){
        String start = null;
        String end = null;
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        OptLogReqVO vo1 = new OptLogReqVO();
        vo1.setOptModule(11);
        vo1.setOptType(8);
       
        vo1.setStatus(1);
        
        if (null != vo.getTime() && !vo.getTime().isEmpty()){
            start = vo.getTime();
            end = vo.getTime() + " 23:59:59";
            vo1.setOptContent("清除"+start+end+"浏览记录");
        }else {
            vo1.setOptContent("清除全部浏览记录");
        }
        logService.saveOptLog(vo1);
        viewHistoryService.delete(start,end,loginUser.getId());
        return new ResponseInfo<>();
    }
    
    @ApiOperation("搜索历史列表")
    @PostMapping("/search-history")
    public ResponseInfo<List<ListHistory>> searchHistory(){
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        List<ListHistory> query = viewHistoryService.query(loginUser.getId());
        return new ResponseInfo<List<ListHistory>>(query);
    }
    
    
    
    @ApiOperation("删除搜索历史")
    @PostMapping("/delete-search")
    public ResponseInfo<Object> deleteSearch(@ApiParam(value = "删除参数") @Valid @RequestBody DeleteSearchVO vo){
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        viewHistoryService.deleteSearch(vo.getId(),loginUser.getId());
        return new ResponseInfo<Object>();
    }
}
